package mpp;

import java.util.Random;

class Ex5q1 {
	private static LockBasedHashTable lockHashTable;
	public static void main(String[] args) {
		int n = 8;
		int lockNumber;
		int loadFactor;

		if (args.length != 2) {
			System.out.println("Wrong number of arguments (Not 2).");
			return;
		}

		try {
			lockNumber = Integer.parseInt(args[0]);
			loadFactor = Integer.parseInt(args[1]);
		} catch (Exception e) {
			System.out.println("Wrong Input");
			return;
		}

		lockHashTable = new LockBasedHashTable(loadFactor, lockNumber);

		Thread[] threads = new Thread[n];

		long start = System.currentTimeMillis();

		// Initialize threads
		for (int i = 0; i < n; i++) {
			threads[i] = new Thread() {
				
				final int ITERATIONS = 10;

				public void run() {
					Random random = new Random();
					for (int j = 0; j < ITERATIONS; j++) {
						lockHashTable.put(random);
					}
				}
			};
		}

		// start the threads
		for (int i = 0; i < n; i++) {
			threads[i].start();
		}

		// waiting for the threads to end
		try {
			for (int i = 0; i < n; i++) {
				threads[i].join();
			}
		} catch (InterruptedException e) {
		}

		System.out.format("Finished after %d miliseconds.\n",
				new Long((System.currentTimeMillis() - start)));
	}
}

class Ex5q2 {
	private static LockBasedHashTable lockHashTable;

	public static void main(String[] args) {
		int n = 8;
		int lockNumber;
		float loadFactor;

		if (args.length != 2) {
			System.out.println("Wrong number of arguments (Not 2).");
			return;
		}

		try {
			lockNumber = Integer.parseInt(args[0]);
			loadFactor = Float.parseFloat(args[1]);
		} catch (Exception e) {
			System.out.println("Wrong Input");
			return;
		}

		lockHashTable = new LockBasedHashTable(loadFactor, 16, lockNumber);

		Thread[] threads = new Thread[n];

		long start = System.currentTimeMillis();

		// Initialize threads
		for (int i = 0; i < n; i++) {
			threads[i] = new Thread() {

				final int ITERATIONS = 1000000;

				public void run() {
					Random random = new Random();
					for (int i = 0; i < ITERATIONS; i++) {
						lockHashTable.put(random);
					}
				}
			};
		}

		// start the threads
		for (int i = 0; i < n; i++) {
			threads[i].start();
		}

		// waiting for the threads to end
		try {
			for (int i = 0; i < n; i++) {
				threads[i].join();
			}
		} catch (InterruptedException e) {
		}

		System.out.format("Finished after %d miliseconds.\n",
				new Long((System.currentTimeMillis() - start)));
	}
}
